package window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import utils.AppUnits;


public class Window extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final JTextArea textArea = new JTextArea();//内容
	public final JCheckBox chkLog = new JCheckBox("写入log日志");
	public final JLabel status = new JLabel();
	public final JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.LEFT));//状态栏
	

	/**
	 * Create the application.
	 */
	public Window() {
		String sfilePath = System.getProperty("java.class.path");
		String sfileName = "";// 程序名
		if (sfilePath.endsWith(".jar")) {
			sfileName = sfilePath.substring(sfilePath.lastIndexOf("\\") + 1,
					sfilePath.length() - 4);
		}

		// 程序名称
		setTitle(sfileName);

		setTitle("dWCS-WMS-API");

		// 程序图标
		// ImageIcon imageIcon = new ImageIcon(getClass().getResource("9.jpg"));
		// WriteLog(getClass().getResource("9.jpg").toString());
		// 设置标题栏的图标为face.gif
		// this.setIconImage(imageIcon.getImage());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 窗口位置
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();// 获得屏幕尺寸
		int width = 320 * 3;// The application's width
		int height = 600 * 1;
		setBounds((d.width - width) / 2, (d.height - height) / 2, width, height);// 窗口的坐标和尺寸，这种方式居中
		

		// 工具栏
		final JToolBar toolBar = new JToolBar("工具栏");// 创建工具栏对象
		toolBar.setFloatable(false);// 设置为不允许拖动
		add(toolBar, BorderLayout.NORTH);
		//清日志
		final JButton clearButton = new JButton("清日志");//
		// 创建按钮对象
		clearButton.addActionListener(new ButtonListener());// 添加动作事件监听器
		toolBar.add(clearButton);// 添加到工具栏中
		toolBar.addSeparator();// 添加默认大小的分隔符
		//写入日志
		toolBar.add(chkLog);
		toolBar.addSeparator();// 添加指定大小的分隔符
		//重启
		final JButton resetButton = new JButton("重启");// 创建按钮对象
		resetButton.addActionListener(new ButtonListener());// 添加动作事件监听器
		toolBar.add(resetButton);// 添加到工具栏中
		toolBar.addSeparator();// 添加默认大小的分隔符

		// 状态栏
		statusBar.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY),
				new EmptyBorder(4, 4, 4, 4)));
		status.setText("1123");
		final JLabel status2 = new JLabel();
		status2.setText("444");
		statusBar.add(status);
		statusBar.add(status2);
		add(statusBar, BorderLayout.SOUTH);

		// 内容
		textArea.setLineWrap(false);
		textArea.setWrapStyleWord(true);
		JScrollPane jScrollPaneInfo = new JScrollPane(textArea);
		add(jScrollPaneInfo);
		// 初始化
		System.out.println("启动");
	}
	
	/**
	 * Launch the application.
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {

		try {
			String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
			UIManager.setLookAndFeel(lookAndFeel);
		} catch (Exception e) {
			e.printStackTrace();
		}

		Window window = new Window();
//		window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//
//		
//		window.setVisible(true);
		window.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (JOptionPane.showConfirmDialog(null, "要关闭吗?", "提示信息",
						JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
					System.exit(0);
				}
			}
		});
		window.textArea.append("hello");
		
			

	}


	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			JButton button = (JButton) e.getSource();
			if (button.getText() == "清日志") {
				textArea.setText("");
			}

			else if (button.getText() == "重启") {
				if (JOptionPane.showConfirmDialog(null, "要重启吗?", "提示信息",
						JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) {
					String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
					System.out.println(path);
					AppUnits.restartApplication(System.getProperty("java.class.path"));
				}

			}
		}
	}


}
